package com.hope.onlinestudy.fragment.user

import com.google.gson.Gson
import com.hope.onlinestudy.base.BaseDataModel
import com.hope.onlinestudy.model.MsgLengthModel
import com.hope.onlinestudy.model.UserInfoModel
import com.hope.onlinestudy.mvp.BasePresenterImpl
import com.hope.onlinestudy.net.HttpNetUtils
import com.hope.onlinestudy.net.NetworkScheduler
import com.hope.onlinestudy.net.ProgressSubscriber
import com.hope.onlinestudy.utils.ApiUtils
import com.hope.onlinestudy.utils.Utils.logs

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class UserPresenter : BasePresenterImpl<UserContract.View>(),
    UserContract.Presenter {

    var userInfo: UserInfoModel? = null

    override fun getMsgAlert() {
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.msgalert}"
        HttpNetUtils.getInstance().getManager()?.getAlertMsg(url,ApiUtils.loginModel?.jwtToken!!)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<MsgLengthModel>(mView) {
                override fun onSuccess(data: MsgLengthModel?, code: Int?) {
                    mView?.setAlertLength(data?.msgCount)
                }
            })
    }

    override fun getUserInfo() {
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.toMyInfo}"
        val msg = Gson().toJson(hashMapOf<String, Any>("userId" to ApiUtils.loginModel?.userId!!))
        HttpNetUtils.getInstance().getManager()?.getUserInfo(url,ApiUtils.loginModel?.jwtToken!!, msg)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<BaseDataModel<List<UserInfoModel>>>(mView) {
                override fun onSuccess(data: BaseDataModel<List<UserInfoModel>>?, code: Int?) {
                    userInfo = data?.data?.get(0)
                    mView?.setInfo(userInfo)
                }
            })
    }

    override fun exitApp() {
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.delecookie}"
        HttpNetUtils.getInstance().getManager()?.deleteSession(url,ApiUtils.loginModel?.jwtToken!!)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<BaseDataModel<String>>(mView) {
                override fun onSuccess(data: BaseDataModel<String>?, code: Int?) {
                    if (data?.statusCode == 0) {
                        mView?.exitLogin()
                    } else {
                        mView?.showMsg(data?.msg!!)
                    }
                }
            })
    }

}